/**
 * Created by admin on 2016/10/31.
 */
document.writeln("<link href=\"../../res/plugins/select2/css/select2.min.css?v=4.0.3\" rel=\"stylesheet\">");
/*共用的js文件*/
document.writeln("<script src=\"../../res/plugins/select2/js/select2.min.js?v=4.0.3\"></script>");


var lbsApi="https://fe.yifengx.com/lbs/RegionService";
var ownOptions={};

function getSelectedCode(provinceId) {
    return $("#"+provinceId+"SelectedCode").val();
}

function getDistrictCode(districtId) {
    return $("#"+districtId).val();
}

function initAreaSelectBySelectedCode(provinceId,cityId,districtId,selectCode) {
    var $province=$('#' + provinceId);
    var $areaSelectedCode=$("#"+provinceId+"SelectedCode");
    if($areaSelectedCode.length===0){
        $('<input type="hidden" id="'+provinceId+'SelectedCode" value="" />').insertBefore($province);
        $areaSelectedCode=$("#"+provinceId+"SelectedCode");
    }
    var $city=$('#' + cityId);
    var $district=$('#' + districtId);
    ownOptions[provinceId]=$province.html();
    ownOptions[cityId]=$city.html();
    ownOptions[districtId]=$district.html();


    var provinceData=queryByParentCode("100000");
    var cityData=null;
    var districtData=null;

    if(selectCode){
        $areaSelectedCode.val(selectCode);
        cityData=queryByParentCode(selectCode.substring(0,2)+"0000");
        districtData=queryByParentCode(selectCode.substring(0,4)+"00");
    }

    bindSelectWithData(provinceId,provinceData);
    bindSelectWithData(cityId,cityData);
    bindSelectWithData(districtId,districtData);
    setSelected(provinceId,cityId,districtId,selectCode)

    $province.on("change",function () {
        var provinceCode=$(this).val();
        $areaSelectedCode.val(provinceCode);
        var cityDataChangeData=null;
        if(provinceCode.length>0){
            cityDataChangeData=queryByParentCode(provinceCode);
        }
        bindSelectWithData(cityId,cityDataChangeData);
        bindSelectWithData(districtId,null);
    });

    $city.on("change",function () {
        var cityCode=$(this).val();
        if (cityCode){
            $areaSelectedCode.val(cityCode);
        }else{
            var proCode=$("#"+provinceId).val();
            $areaSelectedCode.val(proCode);
        }

        var districtDataChangeData=null;
        if(cityCode.length>0){
            districtDataChangeData=queryByParentCode(cityCode);
        }
        bindSelectWithData(districtId,districtDataChangeData);
    });

    $district.on("change",function () {
        var districtCode=$(this).val();
        if(districtCode){
            $areaSelectedCode.val(districtCode);
        }else{
            var ciCode=$("#"+cityId).val();
            $areaSelectedCode.val(ciCode);
        }
    });

}

function  setSelected(provinceId,cityId,districtId,selectCode) {
    var provinceCode="";
    var cityCode="";
    var districtCode="";
    if(selectCode){
        provinceCode=selectCode.substring(0,2)+"0000";
        if(isCity(selectCode)){
            cityCode=selectCode;
        }
        if(isDistrict(selectCode)){
            cityCode=selectCode.substring(0,4)+"00";
            districtCode=selectCode;
        }
    }
    $("#"+provinceId).val(provinceCode);
    $("#"+cityId).val(cityCode);
    $("#"+districtId).val(districtCode);
}

function initAreaSelect(provinceId,cityId,districtId) {
    initAreaSelectBySelectedCode(provinceId,cityId,districtId,null);
}



//绑定字典内容到指定的Select控件
function bindSelectWithData(id, data) {
    var control = $('#' + id);
    //设置Select2的处理
    control.select2({
        allowClear: false,
        formatResult: formatResult,
        formatSelection: formatSelection,
        escapeMarkup: function (m) {
            return m;
        }
    });
    control.empty();
    var ownOption=ownOptions[id];
    if(ownOption){
        control.append(ownOption);
    }
    if((id == "topChannelCode" )|| (id=="add_channel")){
        if(data!=null){
            control.append("<option value=''>所有渠道</option>");
            $.each(data, function (index, obj) {
                control.append("<option value='" + obj.topChannelCode + "' >" + obj.topChannelName + "</option>");
            });
        }
    }else if((id == "deliveryType")|| (id =="add_deliveryType")){
        if(data!=null){
            control.append("<option value=''>--请选择--</option>");
            $.each(data, function (index, obj) {
                if(obj.deliveryType =="URGENT"){
                    control.append("<option value='" + obj.deliveryType + "'>" + "加急" + "</option>");
                }else if(obj.deliveryType =="NORMAL"){
                    control.append("<option value='" + obj.deliveryType + "'>" + "普通" + "</option>");
                }else if(obj.deliveryType =="REAL_TIME"){
                    control.append("<option value='" + obj.deliveryType + "'>" + "实时" + "</option>");
                }else if(obj.deliveryType =="SELF_PICK"){
                    control.append("<option value='" + obj.deliveryType + "'>" + "自提" + "</option>");
                }

            });
        }
    }else if((id == "add_partner")){
        if(data!=null){
            control.append("<option value=''>请选择物流伙伴</option>");
            $.each(data, function (index, obj) {
                control.append("<option value='" + obj.logisticsId + "'>" + obj.name + "</option>");
            });
        }
    }
    else{
        if(data!=null){
            $.each(data, function (index, obj) {
                control.append("<option value='" + obj.code + "'>" + obj.name + "</option>");
            });
        }
    }


}

function queryAreaDetailByAreaCode(areaCode) {
    if (areaCode){
        var areaDatas = CommonUtil.getSessionStorage('lbsDetailData')||{};
        var areaDataByAreaCode=areaDatas[areaCode];
        if (CommonUtil.isEmptyObject(areaDataByAreaCode)) {
            areaDataByAreaCode = getAjaxData("queryDetailByCode",{"code":areaCode});
            if(areaDataByAreaCode){
                areaDatas[areaCode]=areaDataByAreaCode;
                CommonUtil.setSessionStorage("lbsDetailData",areaDatas);
            }

        }
        return areaDataByAreaCode;
    }
    return null;
}


function queryByParentCode(areaCode) {
    if (areaCode==='100000'){
        return getProvinceData();
    }else if(isProvince(areaCode)){
        return getCityData(areaCode);
    }else if(isCity(areaCode)){
        return getDistrictData(areaCode);
    }
    return getAjaxData("queryByParentCode",{"code":areaCode});
}

function getCityData(provinceCode) {
    var cityDatas = CommonUtil.getSessionStorage('lbsCityData')||{};
    var cityDataByProvinceCode=cityDatas[provinceCode];
    if (CommonUtil.isEmptyCollection(cityDataByProvinceCode)) {
        cityDataByProvinceCode = getAjaxData("queryByParentCode",{"code":provinceCode});
        if(cityDataByProvinceCode){
            cityDatas[provinceCode]=cityDataByProvinceCode;
            CommonUtil.setSessionStorage("lbsCityData",cityDatas);
        }
    }
    return cityDataByProvinceCode;
}

function getDistrictData(cityCode) {
    var districtDatas = CommonUtil.getSessionStorage('lbsDisctictData')||{};
    var districtDataByCityCode=districtDatas[cityCode];
    if (CommonUtil.isEmptyCollection(districtDataByCityCode)) {
        districtDataByCityCode = getAjaxData("queryByParentCode",{"code":cityCode});
        if(districtDataByCityCode){
            districtDatas[cityCode]=districtDataByCityCode;
            CommonUtil.setSessionStorage("lbsDisctictData",districtDataByCityCode);
        }
    }
    return districtDataByCityCode;
}


function getProvinceData() {
    var provinceData = CommonUtil.getSessionStorage('lbsProvinceData');
    if (CommonUtil.isEmptyCollection(provinceData)) {
        provinceData = getAjaxData("queryByParentCode",{"code":"100000"});
        CommonUtil.setSessionStorage("lbsProvinceData",provinceData);
    }
    return provinceData;
}

function isProvince(areaCode) {
    if (areaCode.endsWith("0000")){
        return true;
    }
    return false;
}

function isCity(areaCode) {
    if (areaCode.endsWith("00")){
        if (areaCode.endsWith("0000")){
            return false;
        }
        return true;
    }
    return false;
}

function isDistrict(areaCode) {
    if(areaCode.substring(2,4)!='00'){
        if(!areaCode.endsWith("00")){
            return true;
        }
    }
    return false;
}


function getAjaxData(method,param) {
    var result = null;
    CommonUtil.ajax({
        url: lbsApi+'/'+method,
        data:param,
        type: 'app',
        async: false,
        success: function (data) {
            result = data;
        },
        error: function (error) {
        }
    });
    return result;
}




function formatResult() {

}
function formatSelection() {

}